package com.example.neteasecloudmusic_kotlin.ui.homepage

import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.example.neteasecloudmusic_kotlin.R
import com.example.neteasecloudmusic_kotlin.logic.model.HomePageEntity.RootBean
import kotlinx.android.synthetic.main.discovery_playlist.view.*

class DiscoveryPlayListAdapter(val playList: ArrayList<RootBean>, val context: Context) : RecyclerView.Adapter<DiscoveryPlayListAdapter.ViewHolder>() {

    inner class ViewHolder(view: View) : RecyclerView.ViewHolder(view) {
        val playListName = view.tvPlayListName
        val playListItem = view.rvDiscoveryPlayListItem
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        val view = LayoutInflater.from(parent.context).inflate(R.layout.discovery_playlist, parent, false)
        return ViewHolder(view)
    }

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        val playListInfo = playList[position]
        holder.playListName.text = playListInfo.title
        val layoutManager = LinearLayoutManager(context)
        layoutManager.orientation = LinearLayoutManager.HORIZONTAL
        holder.playListItem.layoutManager = layoutManager
        // todo 强制非空 或者 判定
        holder.playListItem.adapter = PlayListItemAdapter(playListInfo.creatives!!)
    }

    override fun getItemCount() = playList.size
}